/*
 * @lc app=leetcode.cn id=128 lang=cpp
 *
 * [128] 最长连续序列
 */
#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>

using std::unordered_map;
using std::vector;
// @lc code=start
class Solution
{
  public:
    int longestConsecutive(vector<int> &nums)
    {
        if (nums.size() == 0) {
            return 0;
        }
        int max_length = -1;
        std::sort(nums.begin(), nums.end());
        int tmp = 1;
        for (int i = 0; i < nums.size() - 1; ++i) {
            if (nums[i + 1] == nums[i] + 1) {
                ++tmp;
            } else if (nums[i + 1] == nums[i]) {
                continue;
            } else {
                max_length = std::max(max_length, tmp);
                tmp = 1;
            }
        }
        return std::max(max_length, tmp);
    }
};
// @lc code=end
int main()
{
    vector<int> nums = {0, 3, 7, 2, 5, 8, 4, 6, 0, 1};
    Solution s;
    std::cout << "result: " << s.longestConsecutive(nums);
}